<?php

namespace SDXApi\Controller;

use Think\Controller;

/**
 * 运营报表_数据接口
 * 
 * @author ^2_3^岑业宝
 *
 */
class ReportapiController extends Controller {
    
    // 预警库存数量
    private $storewarn_num = 3;
    
    // 门店商品库表
    private $arr_wids = array(
        //-/ 高志大厦体验店
        '8' => 'goods_wid_8',
        //-/ 海珠体验店
        '17'    => 'goods_wid_17',
        //-/ 白云体验店
        '18'    => 'goods_wid_18'
    );
    
    public function __construct() {
        parent::__construct();
        
        header("Access-Control-Allow-Origin: *");
        
        /// 简单验证
        if ( !IS_POST ) {
            returnApiError('invalid request;');
        }
        $key = I('request.key', '', 'trim');
        if( empty($key) || $key != '^2_3^' ) {
            returnApiError('invalid request;');
        }
    }
    
    /**
     * 首页
     * @access http://apidev.cyb.cn/index.php?s=/reportapi/index/
     * @author ^2_3^岑业宝
     */
    public function index() {
        $data = array(
            'method' => 'index'
        );
        $msg = 'msg';
        $tip_msg = 'tip_msg';
        returnApiSuccess($data, $msg, $tip_msg);
    }
    
    /**
     * [商品库存预警报表]_获取门店预警商品;
     * @access http://apidev.cyb.cn/index.php?s=/reportapi/get_storewarn_list/ 
     * @author ^2_3^
     */
    public function get_storewarn_list() {
        
        /// 参数处理
        $wid = I('request.wid', 0, 'int');
        if( empty($wid) ) {
            returnApiError('温馨提示: 参数为空;');
            
        }
        $wid_tbl_name = $this->arr_wids[ $wid ];
        
        /// 默认预警库存数量
        $where_gsc = array(
            "set_key" => 'default_num'
        );
        $info_warn = M('gstore_config')->where( $where_gsc )->find();
        if( !empty($info_warn) ) {
            $num_warn = $info_warn['set_val'];
        }else {
            $num_warn = $this->storewarn_num;
        }
        
        /// 列表
//         $list = M('goods g')
//             ->field("
//                 g.goods_id,
//             	g.goods_name,
//             	g.goods_sn,
//             	g.cat_id,
//             	gs.wid,
//             	wh.`name`AS wh_name,
//             	IFNULL(gs.practical_num, 0) AS practical_num,
//             	IFNULL(gsc.set_val, {$num_warn}) AS set_val
//             ")
//             ->join("sdx_goods_storage gs ON gs.goods_id = g.goods_id AND gs.wid IN ({$wid})", 'LEFT')
//             ->join("
//                 sdx_gstore_config gsc ON gsc.set_key = g.goods_sn 
//                 AND gsc.set_type = gs.wid AND gsc.set_type IN ({$wid})
//             ", 'LEFT')
//             ->join("sdx_warehouse wh ON wh.id = gs.wid", 'LEFT')
//             ->where("
//                 IFNULL(gs.practical_num, 0) <= IFNULL(gsc.set_val, {$num_warn}) 
//                 AND gs.wid IN ({$wid}) 
//                 AND g.is_on_sale = 1 
//                 AND g.is_delete = 0 
//             ")
//             ->order("IFNULL(gs.practical_num, 0), g.cat_id")
//             ->select();
        $list = M("{$wid_tbl_name} g")
            ->field("
                g.goods_id,
                g.goods_name,
                g.goods_sn,
                g.cat_id,
                gs.wid,
                wh.`name`AS wh_name,
                IFNULL(gs.practical_num, 0) AS practical_num,
                IFNULL(gsc.set_val, {$num_warn}) AS set_val
                ")
            ->join("sdx_goods_storage gs ON gs.goods_id = g.goods_id AND gs.wid IN ({$wid})", 'LEFT')
            ->join("
                sdx_gstore_config gsc ON gsc.set_key = g.goods_sn
                AND gsc.set_type = gs.wid AND gsc.set_type IN ({$wid})
                ", 'LEFT')
            ->join("sdx_warehouse wh ON wh.id = gs.wid", 'LEFT')
            ->where("
                IFNULL(gs.practical_num, 0) <= IFNULL(gsc.set_val, {$num_warn})
                AND gs.wid IN ({$wid})
                AND g.is_on_sale = 1
                AND g.is_delete = 0
                ")
            ->order("IFNULL(gs.practical_num, 0), g.cat_id")
            ->select();
// w_log("reportapi_log", "get_storewarn_list->sql ".M()->getLastSql());
        
        if( !empty( $list ) ) {
            $arr_result['list'] = $list;
            returnApiSuccess($arr_result);
            
        }else {
            $arr_result['list'] = array();
            returnApiError('温馨提示:没有数据;', $arr_result);
        }
    }
    
}


